#ifndef     __TIMER_H
#define     __TIMER_H

#include "./rtos.h"

namespace rtos {

template <typename M>
class Timer {

public:
    Timer(const char *name = "other") {
        osTimerAttr_t attr;
        attr.name = name;
        attr.attr_bits = 0;
        attr.cb_mem = m_cb_mem;
        attr.cb_size = osRtxTimerCbSize;
        m_id = osTimerNew(timeout, osTimerOnce, nullptr, &attr);
    }

    inline osStatus_t timer_start(uint32_t ticks) {       
        timer_stop();
        return osTimerStart(m_id, ticks);
    }

    inline osStatus_t timer_stop() {
        return osTimerStop(m_id);
    }

private:
    static void timeout(void *self) {
        static_cast<M *>(self)->on_timeout();
    }

private:
    osTimerId_t m_id = nullptr;
    uint64_t m_cb_mem[ (osRtxTimerCbSize + 7) / 8 ];
};

}



#endif
